<template>
    <div class="header" :style="{ backgroundColor: backgroundcolor }">
        <!-- 折叠按钮 -->
        <div class="collapse-btn" @click="collapseChage">
            <i v-if="!collapse" class="el-icon-s-fold"></i>
            <i v-else class="el-icon-s-unfold"></i>
        </div>
        <div class="logo">自有房产管理系统</div>
        <!-- <div class="logo">自有房产管理系统测试版</div> -->
        <div class="header-right">
            <div class="header-user-con">
                <!-- 全屏显示 -->
                <div class="btn-fullscreen" @click="handleFullScreen">
                    <el-tooltip effect="dark" :content="fullscreen ? `取消全屏` : `全屏`" placement="bottom">
                        <i class="el-icon-rank"></i>
                    </el-tooltip>
                </div>
                <!-- <div class="btn-bell">
                    <el-tooltip effect="dark" :content="message ? `有${message}条未读消息` : `消息中心`" placement="bottom">
                        <router-link to="/tabs">
                            <i class="el-icon-bell"></i>
                        </router-link>
                    </el-tooltip>
                    <span class="btn-bell-badge" v-if="message"></span>
                </div> -->
                <!-- 用户头像 -->
                <div class="user-avator">
                    <img src="../../assets/img/header.png" />
                </div>
                <!-- 用户名下拉菜单 @command="handleCommand"-->
                <el-dropdown class="user-name" trigger="click" @command="handleCommand">
                    <span class="el-dropdown-link">
                        {{ username }}
                        <i class="el-icon-caret-bottom"></i>
                    </span>

                    <el-dropdown-menu slot="dropdown">
                        <!-- <el-dropdown-item command="showmessage" @click="handleCommand">{{username}}</el-dropdown-item> -->
                        <el-dropdown-item divided command="changepassword">修改密码</el-dropdown-item>
                        <el-dropdown-item divided command="loginout">退出登录</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </div>
        </div>
        <!-- 用户详情 -->
        <el-dialog title="用户详情" :visible.sync="dialogVisible" :modal-append-to-body="false">
            <el-form ref="detailform" :model="editform" label-width="100px" style="padding-right: 10px">
                <el-form-item label="账号" prop="account">
                    <el-input v-model="detailform.account" disabled></el-input>
                </el-form-item>
                <el-form-item label="单位名称" prop="company">
                    <el-input v-model="detailform.company" disabled></el-input>
                </el-form-item>
                <el-form-item label="用户名" prop="name">
                    <el-input v-model="detailform.name" disabled></el-input>
                </el-form-item>
                <el-form-item label="用户类别" prop="type">
                    <el-input v-model="detailform.type" disabled></el-input>
                </el-form-item>
            </el-form>
        </el-dialog>
        <!-- 修改密码 -->
        <el-dialog title="修改密码" :visible.sync="changeVisible" :modal-append-to-body="false">
            <el-form ref="newpassword" label-width="100px" style="padding-right: 10px">
                <el-form-item label="新密码" prop="account">
                    <el-input v-model="newpassword.password"></el-input>
                </el-form-item>
                <el-form-item label="密码确认" prop="account">
                    <el-input v-model="newpassword.newpasswords"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="changeSubmit('addform')">修改</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>
<script>
import bus from '../common/bus';
import vSidebar from './Sidebar.vue';
import { userdetails, updatePassword } from '@/api/index';
export default {
    data() {
        return {
            detailform: false,
            dialogVisible: false,
            changeVisible: false,
            collapse: false,
            fullscreen: false,
            name: '暂无姓名',
            message: 2,
            backgroundcolor: '#008A94',
            newpassword: {
                password: '',
                newpasswords: '',
                id: this.$store.getters.userId
            },
            editform: {},
            usermsg: {
                CertificateID: '', //证件号
                CertificateName: '', //证件名称
                ContactTel: '', //手机号
                DistrictName: '', //所在区
                HouseKeeperName: '', //用户姓名
                StationName: '' //所在街道
            }
        };
    },
    computed: {
        username() {
            // let username = localStorage .getItem('userName');
            // let username = this.$store.getters.realName;
            let username = this.$store.getters.userName;
            return username ? username : this.name;
        }
    },
    methods: {
        // 获取用户信息
        getusermsg() {
            let data = {
                id: localStorage.getItem('userId')
            };
            userdetails(data).then((res) => {
                console.log(res);
                if (res.code == 200) {
                    this.detailform = res.data;
                } else {
                    this.$message.error(res.message);
                }
            });
        },
        // 修改确定
        changeSubmit() {
            let data = this.newpassword;
            if (this.newpassword.password) {
                if (this.newpassword.newpasswords) {
                    if (this.newpassword.password == this.newpassword.newpasswords) {
                        updatePassword(data).then((res) => {
                            if (res.code == 200) {
                                this.$message.success('请重新登录！！！');
                                this.changeVisible = false;
                                localStorage.clear();
                                this.$router.push('/login');
                                // this.$message.success(res.message);
                            } else {
                                this.$message.error(res.message);
                            }
                        });
                    }else{
                        this.$message.error('新密码与确认密码请保持一致！！！');
                    }
                } else {
                    this.$message.error('请确认密码！！！');
                }
            } else {
                this.$message.error('请填写新密码！！！');
            }
        },
        // 修改用户信息
        changeform() {
            console.log(this.usermsg);
            // 修改信息成功后修改session里面的username值
        },
        // 用户名下拉菜单选择事件
        handleCommand(command) {
            if (command == 'loginout') {
                localStorage.clear();
                this.$router.push('/login');
                // window.location.reload()
            } else if (command == 'showmessage') {
                this.getusermsg();
                this.dialogVisible = true;
            } else if (command == 'changepassword') {
                this.changeVisible = true;
            }
        },
        // 侧边栏折叠
        collapseChage() {
            this.collapse = !this.collapse;
            bus.$emit('collapse', this.collapse);
        },
        // 全屏事件
        handleFullScreen() {
            let element = document.documentElement;
            if (this.fullscreen) {
                if (document.exitFullscreen) {
                    document.exitFullscreen();
                } else if (document.webkitCancelFullScreen) {
                    document.webkitCancelFullScreen();
                } else if (document.mozCancelFullScreen) {
                    document.mozCancelFullScreen();
                } else if (document.msExitFullscreen) {
                    document.msExitFullscreen();
                }
            } else {
                if (element.requestFullscreen) {
                    element.requestFullscreen();
                } else if (element.webkitRequestFullScreen) {
                    element.webkitRequestFullScreen();
                } else if (element.mozRequestFullScreen) {
                    element.mozRequestFullScreen();
                } else if (element.msRequestFullscreen) {
                    // IE11
                    element.msRequestFullscreen();
                }
            }
            this.fullscreen = !this.fullscreen;
        }
    },
    mounted() {
        if (document.body.clientWidth < 1500) {
            this.collapseChage();
        }
    },
    created() {}
};
</script>
<style scoped>
.userspan {
    margin-top: 20px;
}

.header {
    border-bottom: 1px solid #8099ec;
    position: relative;
    box-sizing: border-box;
    width: 100%;
    height: 70px;
    font-size: 22px;
    color: #fff;
}

.collapse-btn {
    float: left;
    padding: 0 21px;
    cursor: pointer;
    line-height: 70px;
}

.header .logo {
    float: left;
    width: 250px;
    line-height: 70px;
}

.header-right {
    float: right;
    padding-right: 50px;
}

.header-user-con {
    display: flex;
    height: 70px;
    align-items: center;
}

.btn-fullscreen {
    transform: rotate(45deg);
    margin-right: 5px;
    font-size: 24px;
}

.btn-bell,
.btn-fullscreen {
    position: relative;
    width: 30px;
    height: 30px;
    text-align: center;
    border-radius: 15px;
    cursor: pointer;
}

.btn-bell-badge {
    position: absolute;
    right: 0;
    top: -2px;
    width: 8px;
    height: 8px;
    border-radius: 4px;
    background: #f56c6c;
    color: #fff;
}

.btn-bell .el-icon-bell {
    color: #fff;
}

.user-name {
    margin-left: 10px;
}

.user-avator {
    margin-left: 20px;
}

.user-avator img {
    display: block;
    width: 40px;
    height: 40px;
    border-radius: 50%;
}

.el-dropdown-link {
    margin-left: 10px;
    color: #fff;
    font-size: 16px;
    /* cursor: pointer; */
}

.el-dropdown-menu__item {
    text-align: center;
}
</style>
